package com.zosoft.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.zosoft.responseResult.Result;

import java.util.List;

/**
 * 后台通用控制器
 * 实现公用方法与实现
 */
public class BackController extends Controller {


    //定义返回结构体,将返回的数据统一封装到 Result中去,
    private Result result;

    public Result getResult() {
        return result;
    }


    public void setResult(Result result) {
        this.result = result;
    }

    public void Result() {
        result = new Result();
        renderJson(result);
    }


    public void Result(int code) {
        result = new Result(code);
        renderJson(result);
    }

    public void Result(int code, Boolean bool) {
        JSONObject obj = new JSONObject();
        obj.put("result", bool);
        result = new Result(obj);
        renderJson(result);
    }

    public void Result(Boolean bool) {
        JSONObject obj = new JSONObject();
        obj.put("result", bool);
        result = new Result(obj);
        renderJson(result);
    }


    public void Result(int code, Page<?> page) {
        result = new Result(code, page);
        renderJson(result);
    }

    public void Result(int code, String value) {
        JSONObject obj = new JSONObject();
        obj.put("result", value);
        Result(obj,code);
    }
    /**
     * 自己输入返回的message
     * @param message 返回的错误提示
     * @param code 状态码
     */
    public void Result(String message,int code) {
      result = new Result(code,message);
      renderJson(result);
	}    

    @SuppressWarnings({"rawtypes", "unchecked"})
    public void Result(int code, List data) {
        JSONArray array = new JSONArray(data);
        result = new Result(code, array);
        renderJson(result);
    }


    @SuppressWarnings("rawtypes")
    public void Result(int code, Model<? extends Model> model) {
        model.removeNullValueAttrs();
        JSONObject obj = JSONObject.parseObject(model.toJson());
        result = new Result(code, obj);
        renderJson(result);
    }


    public void Result(int code, JSONObject data, JSONObject ext) {
        result = new Result(code, data, ext);
        renderJson(result);
    }


    @SuppressWarnings({"rawtypes", "unchecked"})
    public void Result(int code, List data, JSONObject ext) {
        JSONArray array = new JSONArray(data);
        result = new Result(code, array, ext);
        renderJson(result);
    }


    public void Result(Object data) {
        result = new Result(data);
        renderJson(result);
    }
    
    public void Result(Object data,int code ) {
        result = new Result(data);
        result.setresultCode(code);
        renderJson(result);
    }
    
    public void ResultJson(Result data) {
        renderJson(data);
    }

    @SuppressWarnings("rawtypes")
    public void Result(Model<? extends Model> model) {
        model.removeNullValueAttrs();
        JSONObject obj = JSONObject.parseObject(model.toJson());
        result = new Result(obj);
        renderJson(result);
    }

    public void Result(Object data, JSONObject ext) {
        result = new Result(data, ext);
        renderJson(result);
    }

    public void Result(int code, JSONObject ext) {
        result = new Result(code, ext);
        renderJson(result);
    }

    public void ResultError(int code) {
        result = new Result(code);
        renderJson(result);
    }

    /**
     * 进货仓库编号
     * @return
     */
    public String getWarehouseNo(){
    	return "1505198322490001";
    }
    /**
     * 进货仓库名称
     * @return
     */
    public String getWarehouseName(){
    	return "郑洲仓储中心";
    }
    
    /**
     * 根据费用判断运费
     * @param amount
     * @return
     */
    public double getFreight(double amount){
    	if(amount==0){
    		return 3;
    	}else{
    		return amount>=40?0:3;
    	}
    }
}
